<?php

/**
 * This is the model class for table "sys_serial_buyer".
 *
 * The followings are the available columns in table 'sys_serial_buyer':
 * @property string $serial_buyer_id
 * @property string $serial_buyer_serial_id
 * @property string $serial_buyer_network_id
 * @property string $serial_buyer_datetime
 * @property string $serial_buyer_is_cancelled
 */
class sys_serial_buyer extends CActiveRecord {

    public $member_name;
    public $network_mid;
    public $count_active;
    public $count_pasive;
    public $total_pasive_active;
    /**
     * Returns the static model of the specified AR class.
     * @return sys_serial_buyer the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'sys_serial_buyer';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('serial_buyer_serial_id', 'length', 'max' => 20),
            array('member_name, network_mid', 'length', 'max' => 40),
            array('serial_buyer_network_id,count_pasive,count_active', 'length', 'max' => 10),
            array('count_pasive, count_active', 'length', 'max' => 3),
            array('serial_buyer_is_cancelled', 'length', 'max' => 1),
            array('serial_buyer_datetime', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('count_pasive, count_active, member_name, network_mid, serial_buyer_id, serial_buyer_serial_id, serial_buyer_network_id, serial_buyer_datetime, serial_buyer_is_cancelled', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'rel_buyer_to_serial'=>array(self::BELONGS_TO,'sys_serial','serial_buyer_serial_id'),
            'rel_buyer_to_network'=>array(self::BELONGS_TO,'sys_network','serial_buyer_network_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'serial_buyer_id' => 'Serial Buyer',
            'serial_buyer_serial_id' => 'Serial Number',
            'serial_buyer_network_id' => 'Buyer',
            'serial_buyer_datetime' => 'Serial Buy Datetime',
            'serial_buyer_is_cancelled' => 'Serial Buy Is Cancelled',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('serial_buyer_id', $this->serial_buyer_id, true);
        $criteria->compare('serial_buyer_serial_id', $this->serial_buyer_serial_id, true);
        $criteria->compare('serial_buyer_network_id', $this->serial_buyer_network_id, true);
        $criteria->compare('serial_buyer_datetime', $this->serial_buyer_datetime, true);
        $criteria->compare('serial_buyer_is_cancelled', $this->serial_buyer_is_cancelled, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }
    
    public function search_serial_report()
    {
        $criteria= new CDbCriteria;
        
        $convert_date=new ConvertDateHelper;   
        
        /*
         * DEFAULT WAKTU KEMARIN DAN SEKARANG
         */
        $start=date('Y-m-d',  mktime(0, 0, 0, date('m'), date('d')-1, date('Y')));
        $end=date('Y-m-d');
        if(isset($_GET['sys_serial_buyer']))
        {
            if(isset($_GET['start_date'],$_GET['end_date']) AND trim($_GET['start_date'])!='' AND trim($_GET['end_date'])!='')
            {
                     $start=$convert_date->explode_datetime_iso('/', $_GET['start_date'], $type="date");
                     $end=$convert_date->explode_datetime_iso('/', $_GET['end_date'], $type="date");
       
            }
        }
        
        
        $criteria->select=' t.*,
                            (SELECT COUNT(*) FROM sys_serial_buyer sub_ssb 
                            LEFT JOIN sys_serial sub_ss ON sub_ss.serial_id=sub_ssb.serial_buyer_serial_id
                            WHERE sub_ssb.serial_buyer_network_id=t.serial_buyer_network_id
                            AND sub_ss.serial_is_active=\'0\' AND sub_ss.serial_is_used=\'0\'
                            AND sub_ssb.serial_buyer_datetime=t.serial_buyer_datetime
                            ) AS count_pasive,
                            (SELECT COUNT(*) FROM sys_serial_buyer sub_ssb 
                            LEFT JOIN sys_serial sub_ss ON sub_ss.serial_id=sub_ssb.serial_buyer_serial_id
                            WHERE sub_ssb.serial_buyer_network_id=t.serial_buyer_network_id
                            AND sub_ss.serial_is_active=\'1\' AND sub_ss.serial_is_used=\'1\'
                            AND sub_ssb.serial_buyer_datetime=t.serial_buyer_datetime
                            
                            ) AS count_active
                            ';
        
        $criteria->addBetweenCondition('t.serial_buyer_datetime', $start, $end);
        $criteria->with=array('rel_buyer_to_serial','rel_buyer_to_network',
                                'rel_buyer_to_network.rel_network_to_member',);
        $criteria->together=true;
        $criteria->group='t.serial_buyer_network_id, t.serial_buyer_datetime';
        
        $criteria->compare('serial_buyer_id', $this->serial_buyer_id, true);
        $criteria->compare('serial_buyer_serial_id', $this->serial_buyer_serial_id, true);
        $criteria->compare('serial_buyer_network_id', $this->serial_buyer_network_id, true);
        $criteria->compare('serial_buyer_datetime', $this->serial_buyer_datetime, true);
        $criteria->compare('serial_buyer_is_cancelled', $this->serial_buyer_is_cancelled, true);
        $criteria->compare('rel_network_to_member.member_name', $this->member_name, true);
        $criteria->compare('rel_buyer_to_network.network_mid', $this->network_mid, true);
        $criteria->compare('(SELECT COUNT(*) FROM sys_serial_buyer sub_ssb 
                            LEFT JOIN sys_serial sub_ss ON sub_ss.serial_id=sub_ssb.serial_buyer_serial_id
                            WHERE  sub_ssb.serial_buyer_network_id=t.serial_buyer_network_id
                            AND sub_ss.serial_is_active=\'0\' AND sub_ss.serial_is_used=\'0\'
                            AND sub_ssb.serial_buyer_datetime=t.serial_buyer_datetime
                            )', $this->count_pasive, false);
        $criteria->compare('(SELECT COUNT(serial_buyer_id) FROM sys_serial_buyer sub_ssb 
                            LEFT JOIN sys_serial sub_ss ON sub_ss.serial_id=sub_ssb.serial_buyer_serial_id
                            WHERE sub_ssb.serial_buyer_network_id=t.serial_buyer_network_id
                            AND sub_ss.serial_is_active=\'1\' AND sub_ss.serial_is_used=\'1\'
                            AND sub_ssb.serial_buyer_datetime=t.serial_buyer_datetime
                            )', $this->count_active, false);

        
        $data=new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            
        ));
        $_SESSION['data_export']=$data;
        return $data;
        
    }

}